package com.songxh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.common.R;
import com.songxh.dto.DishDto;
import com.songxh.pojo.Category;
import com.songxh.pojo.Dish;
import com.songxh.pojo.DishFlavor;
import com.songxh.pojo.Setmeal;
import com.songxh.service.CategoryService;
import com.songxh.service.DishFlavorService;
import com.songxh.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songxh
 * @create 2022-04-25-16:05
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;


    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dtoPage = new Page<>();

        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        queryWrapper.orderByAsc("sort");
        dishService.page(pageInfo,queryWrapper);


        //获取分类的名字
        //将pageInfo当中除了records集合之外的所有数据拷贝到dtoPage中
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Dish> records = pageInfo.getRecords();//records集合中存放这每一条数据的内容
        List<DishDto> list=new ArrayList<>();

        for (Dish record : records) {//获取records中每一个数据的分类名并将该值赋值给dto中
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(record,dto);

            Long categoryId = record.getCategoryId();//获取分类id
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();//得到分类的名字
            dto.setCategoryName(categoryName);//设置fto中的分类名

            list.add(dto);
        }

        dtoPage.setRecords(list);//将修改分类名后的集合赋给dtoPage

        return R.success(dtoPage);
    }


    /**
     * 添加菜品功能
     * @param dishDto  专门用来接收客户端传来的数据的一个实体类
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("添加成功");
    }


    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable long id){

        //自己写的业务功能，具体代码在业务层
        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }


    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }


    @PostMapping("/status/{status}")
    public R<String> status(@RequestParam("ids") List<Long> ids,@PathVariable int status){
        UpdateWrapper<Dish> wrapper=new UpdateWrapper<>();
        wrapper.in("id",ids);
        wrapper.set("status",status);
        dishService.update(wrapper);


        return R.success("修改成功");
    }


    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        for (Long id : ids) {
            dishService.removeById(id);
        }
        return R.success("删除成功");
    }


    /**
     * 添加套餐时，需要展示每个菜品有哪些菜
     * 前台首页展示套餐时会用上
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(long categoryId){
        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
        //查询状态为”起售“
        queryWrapper.eq("status",1);
        queryWrapper.eq("category_id",categoryId);
        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> dtoList=new ArrayList<>();

        for (Dish dish : list) {
            DishDto dto = new DishDto();
            BeanUtils.copyProperties(dish,dto);

            Long dishCategoryId = dish.getCategoryId();//获取分类id
            Category category = categoryService.getById(dishCategoryId);
            String categoryName = category.getName();//得到分类的名字
            dto.setCategoryName(categoryName);//设置fto中的分类名

            //设置DishDto中的Flavors属性
            Long id = dish.getId();
            QueryWrapper<DishFlavor> flavorQueryWrapper=new QueryWrapper<>();
            flavorQueryWrapper.eq("dish_id",id);
            List<DishFlavor> dishFlavors = dishFlavorService.list(flavorQueryWrapper);
            dto.setFlavors(dishFlavors);

            dtoList.add(dto);
        }

        return R.success(dtoList);
    }



}
